﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Mewdam.Twitter
{
    public static class TwitterUtility
    {

        public static string BuildReadableHtml(string xmlString)
        {
            List<Status> statuses = new List<Status>();
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xmlString);
            foreach (XmlNode node in xdoc.ChildNodes[1].ChildNodes)
            {
                Status status = new Status();
                status.created_at = node.SelectSingleNode("created_at").InnerText;
                status.id = node.SelectSingleNode("id").InnerText;
                status.text = node.SelectSingleNode("text").InnerText;
                status.source = node.SelectSingleNode("source").InnerText;
                status.truncated = (node.SelectSingleNode("truncated").InnerText == "true" ? true : false);
                status.favorited = (node.SelectSingleNode("favorited").InnerText == "true" ? true : false);
                status.in_reply_to_status_id = node.SelectSingleNode("in_reply_to_status_id").InnerText;
                status.in_reply_to_user_id = node.SelectSingleNode("in_reply_to_user_id").InnerText;
                status.in_reply_to_screen_name = node.SelectSingleNode("in_reply_to_screen_name").InnerText;
                int.TryParse(node.SelectSingleNode("retweet_count").InnerText, out status.retweet_count);
                status.retweeted = (node.SelectSingleNode("retweeted").InnerText == "true" ? true : false);
                //status.id = node.SelectSingleNode("id").InnerText;

                long.TryParse(node.SelectSingleNode("user/id").InnerText, out status.user.id);
                status.user.name = node.SelectSingleNode("user/name").InnerText;
                status.user.screen_name = node.SelectSingleNode("user/screen_name").InnerText;
                status.user.location = node.SelectSingleNode("user/location").InnerText;
                status.user.description = node.SelectSingleNode("user/description").InnerText;
                status.user.profile_image_url = node.SelectSingleNode("user/profile_image_url").InnerText;
                status.user.url = node.SelectSingleNode("user/url").InnerText;
                status.user._protected = (node.SelectSingleNode("user/protected").InnerText == "true" ? true : false);
                int.TryParse(node.SelectSingleNode("user/followers_count").InnerText, out status.user.followers_count);
                status.user.profile_background_color = node.SelectSingleNode("user/profile_background_color").InnerText;
                status.user.profile_text_color = node.SelectSingleNode("user/profile_text_color").InnerText;
                status.user.profile_link_color = node.SelectSingleNode("user/profile_link_color").InnerText;
                status.user.profile_sidebar_fill_color = node.SelectSingleNode("user/profile_sidebar_fill_color").InnerText;
                status.user.profile_sidebar_border_color = node.SelectSingleNode("user/profile_sidebar_border_color").InnerText;
                status.user.friends_count = node.SelectSingleNode("user/friends_count").InnerText;
                status.user.created_at = node.SelectSingleNode("user/created_at").InnerText;
                int.TryParse(node.SelectSingleNode("user/favourites_count").InnerText, out status.user.favourites_count);
                int.TryParse(node.SelectSingleNode("user/utc_offset").InnerText, out status.user.utc_offset);
                status.user.time_zone = node.SelectSingleNode("user/time_zone").InnerText;
                status.user.profile_background_image_url = node.SelectSingleNode("user/profile_background_image_url").InnerText;
                status.user.profile_background_tile = node.SelectSingleNode("user/profile_background_tile").InnerText;
                status.user.profile_use_background_image = node.SelectSingleNode("user/profile_use_background_image").InnerText;
                status.user.notifications = (node.SelectSingleNode("user/notifications").InnerText == "true" ? true : false);
                status.user.geo_enabled = (node.SelectSingleNode("user/geo_enabled").InnerText == "true" ? true : false);
                status.user.verified = (node.SelectSingleNode("user/verified").InnerText == "true" ? true : false);
                status.user.following = (node.SelectSingleNode("user/following").InnerText == "true" ? true : false);
                int.TryParse(node.SelectSingleNode("user/statuses_count").InnerText, out status.user.statuses_count);
                status.user.lang = node.SelectSingleNode("user/lang").InnerText;
                status.user.contributors_enabled = (node.SelectSingleNode("user/contributors_enabled").InnerText == "true" ? true : false);
                status.user.follow_request_sent = (node.SelectSingleNode("user/follow_request_sent").InnerText == "true" ? true : false);
                int.TryParse(node.SelectSingleNode("user/listed_count").InnerText, out status.user.listed_count);
                status.user.show_all_inline_media = (node.SelectSingleNode("user/show_all_inline_media").InnerText == "true" ? true : false);

                statuses.Add(status);
            }



            string htmlString=string.Empty;

            foreach (Status s in statuses)
            {
                htmlString += "<b>" + s.user.name+"("+s.user.screen_name+")" + "</b><br />";
                htmlString += s.text+"<br />";
                htmlString += s.created_at + " " + s.source;
                htmlString += "<p>";

            }

            return htmlString;
        }
    }
}